//
//  WSPlayerTool.m
//  RunningMan
//
//  Created by 王帅 on 16/5/23.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "WSPlayerTool.h"

@interface WSPlayerTool ()
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation WSPlayerTool

+ (WSPlayerTool *)shareMusic {
    static  WSPlayerTool *play =nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        play = [[WSPlayerTool alloc]init];
    });
    
    return play;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _player = [[AVPlayer alloc] init];
        
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playNextMusic) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
    return self;
}

-(void)playNextMusic {
    
    if ([self.delegate respondsToSelector:@selector(automaticPlayNextMusic)]){
        
        [self.delegate automaticPlayNextMusic];
    }
    
    
}

//播放
- (void)playMusic {
    [self.player play];
    
    if (self.timer == nil) {
        
        //自动开启
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(starTimer) userInfo:nil repeats:YES];
    } else {
        return;
    }
    
}
//暂停
- (void)pauseMusic {
    
    [self.player pause];
    
    [self stopTimer];
}

//准备播放
- (void)preparePlayMusicWithUrl:(NSString *)url {
    
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:url]];
    // 当前播放的item
    [self.player replaceCurrentItemWithPlayerItem:item];
    
    [self playMusic];
    
    
}

//是否在播放
- (BOOL)isPlaying {
    // 当前播速度
    return self.player.rate;
    
}

//获取当前时间
- (NSInteger)currentTime {
    
    CMTime time = self.player.currentTime;
    // timescale 时间量程
    if (time.timescale == 0) {
        return 0;
    }
    // 返回当前时间(秒)
    return time.value / time.timescale;
    
}

//总时间
- (NSInteger)totalTime {
    CMTime time = self.player.currentItem.duration;
    if (time.timescale == 0) {
        return 0;
    }
    
    return time.value / time.timescale;
}

//把时间转化为字符串
- (NSString *)changeSecondToTime:(NSInteger)second {
    
    NSInteger minute = second / 60;
    NSInteger seconds = second % 60;
    // @"%02ld:%02ld"   0:自动补齐  2:宽度
    return [NSString stringWithFormat:@"%02ld:%02ld", minute, seconds];
    
}

//定时器方法
-(void)starTimer {
    // 响应方法
    if ([self.delegate respondsToSelector:@selector(playMusicCurrenTime:totalTime:Progress:)]) {
        
        NSString * current = [self changeSecondToTime:[self currentTime]];
        NSString *total = [self changeSecondToTime:[self totalTime]];
        CGFloat progress = [self fechTotalTime];
        // 协议方法
        [self.delegate playMusicCurrenTime:current totalTime:total Progress:progress];
        
    }
    
}

//关闭定时器
-(void)stopTimer {
    
    [self.timer invalidate];
    self.timer = nil;
}

//滑块
-(CGFloat)fechTotalTime {
    CGFloat current = (CGFloat)[self currentTime];
    CGFloat total = (CGFloat)[self totalTime];
    
    return current/total;
}

@end
